package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjPlanDao;
import cn.topevery.garden.entity.po.ProjPlan;
import cn.topevery.garden.entity.query.ProjPlanQuery;
import cn.topevery.garden.entity.vo.ProjPlanAndFilesVO;
import cn.topevery.garden.entity.vo.ProjPlanAndProjNameVO;
import cn.topevery.garden.entity.vo.ProjPlanIndexVO;
import cn.topevery.garden.entity.vo.ProjPlanVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.Value;
import hw.topevery.framework.enums.DbTypeEnum;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * 项目计划表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjPlanDaoImpl extends BaseDaoImpl<ProjPlan> implements ProjPlanDao {
    @Override
    public <P extends DbCsp> List<ProjPlan> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<ProjPlanVO> listPage(ProjPlanQuery query) {
        Integer pageIndex = query.pageIndex - 1;

        StringBuffer table = new StringBuffer();
        table.append(" t_proj_plan tpp ");
        table.append("  inner join t_proj_basic_info tpbi on tpbi.c_proj_id = tpp.c_proj_id");

        String columns = "tpp.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table.toString()).setColumns(columns).setOrderBy("tpp.c_id desc");
        sqlQueryMate
                .where(true, "tpp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tpbi.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(query.getProjId()), "tpbi.c_proj_id", ScriptConditionEnum.Eq, query.getProjId())
                .where(ObjectUtil.isNotEmpty(query.getProjName()), "tpbi.c_proj_name", ScriptConditionEnum.Like, query.getProjName())
        ;

        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjPlanVO.class);
    }

    @Override
    public List<ProjPlanAndProjNameVO> list(ProjPlanQuery query) {
        StringBuffer table = new StringBuffer();
        table.append(" t_proj_plan tpp ");
        table.append("  inner join t_proj_basic_info tpbi on tpbi.c_proj_id = tpp.c_proj_id");

        String columns = "tpp.*, tpbi.c_proj_name";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table.toString()).setColumns(columns).setOrderBy("tpp.c_id desc");
        sqlQueryMate
                .where(true, "tpp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tpbi.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(ObjectUtil.isNotEmpty(query.getProjId()), "tpbi.c_proj_id", ScriptConditionEnum.Eq, query.getProjId())
                .where(ObjectUtil.isNotEmpty(query.getProjName()), "tpbi.c_proj_name", ScriptConditionEnum.Like, query.getProjName())
                .where(ObjectUtil.isNotEmpty(query.getIds()), "tpp.c_id", ScriptConditionEnum.In, query.getIds())
        ;
        return selectList(sqlQueryMate, ProjPlanAndProjNameVO.class);
    }

    @Override
    public void saveOrUpdate(String userId, Class<ProjPlan> entityClass, ProjPlan entity) {

    }

    @Override
    public void saveOrUpdate(String userId, Class<ProjPlan> entityClass, Collection<ProjPlan> list) {

    }

    @Override
    public ProjPlanVO getEntityById(String planId) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select tpp.*, tpbi.c_proj_name ");
        sql.append("   from t_proj_plan tpp ");
        sql.append("  inner join t_proj_basic_info tpbi on tpbi.c_proj_id = tpp.c_proj_id ");
        sql.append("  where tpp.c_plan_id = :p_planId ");

        Value<ProjPlanVO> res = new Value<>();
        run(sql.toString(), CommandType.Text, dbCmd -> {
            dbCmd.addInParameter("p_planId", DbTypeEnum.VARCHAR, planId);
            res.data = dbCmd.executeToEntity(ProjPlanVO.class);
        });
        return res.data;
    }

    @Override
    public List<ProjPlanIndexVO> listByProjId(String projId) {
        StringBuffer table = new StringBuffer();
        table.append(" t_proj_plan tpp ");

        String columns = "tpp.*," +
                "to_days(tpp.c_plan_end_time) - to_days(tpp.c_plan_begin_time) c_plan_days, " +
                "to_days(tpp.c_real_end_time) - to_days(tpp.c_real_begin_time) c_real_days ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table.toString()).setColumns(columns).setOrderBy("tpp.c_db_create_date asc");
        sqlQueryMate
                .where(true, "tpp.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tpp.c_proj_id", ScriptConditionEnum.Eq, projId)
        ;
        return selectList(sqlQueryMate, ProjPlanIndexVO.class);
    }
}
